package com.gdpu.project.action.achievementAnalyse;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.project.service.ChengGuoService;
import com.gdpu.project.util.excel.WriteExcel;
import com.gdpu.project.util.xml.FormatXML;
import com.gdpu.project.util.xml.Pie3DXML;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AnalyseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String condition;
	private String achievementType;
	private HttpServletRequest request;
	private HttpSession session;
	private ChengGuoService chengguoService;

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getAchievementType() {
		return achievementType;
	}

	public void setAchievementType(String achievementType) {
		this.achievementType = achievementType;
	}

	public ChengGuoService getChengguoService() {
		return chengguoService;
	}

	public void setChengguoService(ChengGuoService chengguoService) {
		this.chengguoService = chengguoService;
	}

	// 分析
	public String analyseAchievement() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		String path=ServletActionContext.getServletContext().getRealPath("/")+"admin"+File.separator+"achievementAnalyse"+File.separator+"Gallery"+File.separator+"Data"+File.separator+"Pie3D.xml";
		System.out.println(path);
		Map<String, String> map = new HashMap<String, String>();
		System.out.println("getCondition====="+getCondition());		
		System.out.println("getAchievementType====="+getAchievementType());
		map = chengguoService.analyse(getCondition().trim(),getAchievementType().trim());
		//map = chengguoService.analyse("department","CgProject");
		Pie3DXML.createXML(map, path,this.achievementType,this.condition);
		int mapsize = map.size();
		Iterator it = map.entrySet().iterator();
		for (int i = 0; i < mapsize; i++)
		{
			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();
			System.out.println("所属部门："+key);
			System.out.println("统计数:"+value);
		}
		FormatXML.formatXml(path);
		String xmlValue=Pie3DXML.readXML(path);
		if("CgProject".equals(this.achievementType)){
			request.setAttribute("achievementType", "科研项目");
		}else if("CgThesis".equals(this.achievementType)){
			request.setAttribute("achievementType", "论文");			
		}else if("CgConference".equals(this.achievementType)){
			request.setAttribute("achievementType", "学术会议");			
		}else if("CgInnovate".equals(this.achievementType)){
			request.setAttribute("achievementType", "指导学生科技创新");			
		}else if("CgPatent".equals(this.achievementType)){
			request.setAttribute("achievementType", "专利");			
		}else{
			request.setAttribute("achievementType", "专著");		
		}
		request.setAttribute("condition","department".equals(condition)?"按教研室":"postion".equals(condition)?"按职称":"education"
					.equals(condition)?"按学历":"");
		request.setAttribute("xmlValue", xmlValue);
		return SUCCESS;
	}
	
	
	// 报表导出
	public String exportExcel() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		String path=ServletActionContext.getServletContext().getRealPath("/")+"export/download.xls";
		ArrayList<String[]> studentList = new ArrayList<String[]>();
		String tiltes[] = { "学号", "姓名", "班级", "就业方向", "职业", "就业地点", "工作职位",
				"工作时间" };
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			String student[] = { String.valueOf(i), "软件", ".net工程师", "梁飞龙",
					"07计算机医学应用（1）", "广东省中山市中山韦达信息技术有限公司", "Java工程师", "半年" };
			studentList.add(student);
		}
		long time = System.currentTimeMillis();
		System.out.println((time - startTime) + "ms");
		try {
			WriteExcel.write(tiltes, path, "项目信息统计", studentList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
